/*! @file mainwindow.h
  * @brief Definition of MainWindow Class
  * @author AK
  * @date 26.05.2012
  */

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include "ui_mainwindow.h"
#include "binaryviewwidget.h"
#include "decryptdlg.h"
#include "defines.h"
#include "extdevice.h"
#include "extdevicedlg.h"
#include "sqldlg.h"
#include "sqlite3.h"
#include "sqlitedb.h"
#include "timeconvdlg.h"

#include <QMainWindow>
#include <QFileInfo>
#include <QStatusBar>
#include <QProgressBar>
#include <QMessageBox>
#include <QStandardItemModel>


/*! @brief MainWindow Class
  * This is the main class of the program
  */
class MainWindow : public QMainWindow, private Ui::MainWindow
{
    Q_OBJECT

public:
    MainWindow(QString,                 // language
               QPoint topLeft,          // Window position
               QSize size,              // Window size
               QWidget *parent = 0);    // Constructor used w/o CLI Args
    MainWindow(QString,                 // args.at(1)
               QString,                 // language
               QPoint,
               QSize,
               QWidget *parent = 0);    // Constructor used with CL Args
    ~MainWindow();                      // dtor

    inline void setWindowPositionAndSize(const QRect& fg) {resize(fg.size()); move(fg.topLeft());}
    inline QRect windowPositionAndSize() const { return QRect(pos(), size()); }

    QList<QString> allFiles;            //! files found in recursive scan of dir
    QString dbName;                     //! Name of DB to be used
    QList<QString> header;              //! Holds Column Names
    QStatusBar *myStatusBar;            //! just a statusbar
    QVector<SqliteDb *> sqlitedb;       //! QVector of sqliteDb Objects
    QLabel *status;                     //! Statusbar message

public Q_SLOTS:

Q_SIGNALS:
    void resetItemCount();              // sent to dbFilesTreeWidget

private:
    Ui::MainWindow ui;

    QString currLang;                   // TODO
    sqlite3 *db;                        // Handle to DB
    QString dbDir;                      // directory for rec. scan
    QString dbType;                     // places.sqlite, mmssms.db, ...
    bool doDates;                       // do date filtering
    ExtDevice *extDevice;               // external Android Phone object
    ExtDeviceDlg *extDevDlg;            // external Device Dialog window object
    QDate from;                         // filter from
    BinaryViewWidget *hexView;          // hexView object
    QString keyDir;                     // dir including key3.db and cert8.db
    QString lang;                       // TODO
    QTranslator m_translator;           // TODO
    QStandardItemModel *model;          // ItemModel for tableView_0
    QMessageBox msgBox;                 // hmm, what will this be? ;-)
    QProgressDialog *pd;
    ResultSet resultset;
    int rc;
    QString searchColumn;               // filter column
    QString searchTerms;                // filter searchTerms
    int tableTreeViewRowCount;          // number of tables in db and treeView
    SqliteDb *temp;                     // Database object
    QString templateFile;               // holds template filename
    QString templatePath;               // holds templates path
    TimeConvDlg *timeConvDlg;
    QString timeFormat;                 // format string 4 time functs
    QDate to;                           // filter to
    QStandardItemModel *tree;           // ItemModels for dbFileTreeView

    void    buildFileTables();              // Builds TreeWidget on Page 2
    QString createSqlStmt(QString,          // table
                          QList<QString>,   // fields
                          const int);   // type @sa FileType namespace
    void    displayError(QString,          // setText of msgBox
                         QString,          // InformativeText
                         QString);         // sql_qry
    int     isBase64(const char *);         // used in deCrypt()
    void    prepareSignalSlot();           // signal slot connections
    int     openDb(const int,               // mode @sa DbDlg namespace
                   const int);              // force open Dialog or not
    int     queryDb(sqlite3 * tmp,          // handle to db to be queried
                    const int type,         // type @sa FileType namespace
                    const double size);     // size in MB
    void    recurseDirectory(const QString &);     // root directory

private Q_SLOTS:
    void clearSqlFieldAct();            // clears SqlEditField on Page 3
    void closeTab(int idx);             // close tab(idx)
    void copy();                        // copy result View to QClipboard
    QList<QString> deCrypt(QString keyDir,      // directory with key3.db, cert8 file aso.
                           QString hostUrl,       // cleartext Host Url
                           QString ciphUName,     // cipher UserName
                           QString ciphPWord);    // cipher Password
    void deCryptAllAct();               // on Button clicked
    void getSearch(QString,             // header
                   bool,                // do Date searches
                   QString,             // searchTerms
                   QDate,               // from
                   QDate);              // to
    void getSql(QString);               // get query from tab1 TextEdit
    void log(Qt::GlobalColor,
             const QString &);          // logs a message to outputTextEdit
    void mountExtDeviceAct();
    void on_colSelButtonClicked();      // select columns to timeconvert
    void on_expoReady(QString baseDir,  // sent from ExtDevice
                      QString expoDir,
                      QString deviceId);
    void on_gotDevice(QString dev);     // sent from ExtDeviceDlg
    void on_gotTimeConv(int);           // sent from TimeConvDlg, (int @sa FileType definition)
    void on_langEngAct_triggered();
    void on_langGerAct_triggered();
    void openDbAct();
    void openFileAct();                 // needed to catch returnPressed()
    void openFolderAct();
    void openHexViewAct();
    void openHexViewActCanceled();
    void openTemplate(QTreeWidgetItem*, int);   // open file template
    void parseIt();                // parse the opened file -> BinaryViewWidget
    void queryDbAct();
    int  queryFinished(ResultSet);      // QueryThread finished
    void queryStarted();                // QueryThread started
    void rightBtnConvertAct();          // context menu item
    void rightBtnCopyAct();             // context menu item
    void rightBtnDeCryptAct();          // context menu item
    void rightBtnFilterAct();           // context menu item
    void rightBtnOpenUrlAct();          // context menu item
    void saveTemplate();                // saves settings to template
    void saveTemplate(QString dir,      // saves app settings
                      QString file);
    void saveWindowState();
    void showContextMenu(const QPoint & pos);   // right click menu in tableView_0
    void showSearchWindow();            // show dialog
    void showSqlWindow();
};

#endif // MAINWINDOW_H
